package com.yiyi.credit.manage.sso.mapper;

import com.yiyi.credit.manage.sso.common.constants.PaginationConstants;
import com.yiyi.credit.manage.sso.common.page.PageUtils;
import org.apache.commons.lang.StringUtils;

import java.util.Map;


public class SysUserAllInfoSqlProvider {

    //查询表
    private String fromInfo = " from sys_user_role g left join sys_role r on g.role_id=r.id left join sys_user u on g.user_id=u.id left join sys_user_info ui on ui.uid=u.id";


    public String queryForPage(Map<String, Object> paramMap) {
        String selectFiled = "g.user_id id,r.id roleId, ui.real_name realName,u.user_name userName,ui.phone phone,r.role_name roleName,u.status";

        return PageUtils.getPageCommonSql(selectFiled, fromInfo, whereSql(paramMap), "id")
                + PaginationConstants.LIMIT_SQL;
    }




    public String queryUser(Map<String, Object> paramMap) {
        String selectFiled = "g.user_id id, ui.real_name realName,u.user_name userName,ui.phone phone,r.role_name roleName";

        return PageUtils.getPageCommonSql(selectFiled, fromInfo, whereSqlById(paramMap), "id");
    }



    public String queryCount(Map<String, Object> paramMap) {
        String countFiled = " count(g.user_id) count";
        return PageUtils.getPageCountSql(countFiled, fromInfo, whereSql(paramMap));
    }


    private String whereSql(Map<String, Object> paramMap) {
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append(" WHERE 1 = 1 ");
        if (null != paramMap.get("sysInfoId")
                && StringUtils.isNotBlank(String.valueOf(paramMap.get("sysInfoId")))) {
            sqlBuilder.append(" AND g.sys_info_id = #{sysInfoId} ");
        }
        if (null != paramMap.get("userName")
            && StringUtils.isNotBlank(String.valueOf(paramMap.get("userName")))) {
            sqlBuilder.append(" AND u.user_name like concat('%', #{userName}, '%') ");
        }
        if (null != paramMap.get("roleName")
            && StringUtils.isNotBlank(String.valueOf(paramMap.get("roleName")))) {
            sqlBuilder.append(" AND ui.real_name like concat('%', #{roleName}, '%') ");
        }
        if (null != paramMap.get("roleNameId")
            && StringUtils.isNotBlank(String.valueOf(paramMap.get("roleNameId")))) {
            sqlBuilder.append(" AND r.id = #{roleNameId} ");
        }
        if (null != paramMap.get("status")
            && StringUtils.isNotBlank(String.valueOf(paramMap.get("status")))) {
            String status=paramMap.get("status").toString();
            if(Integer.parseInt(status)!=-1) {
                sqlBuilder.append(" AND u.status = #{status} ");
            }
        }
        return sqlBuilder.toString();
    }



    private String whereSqlById(Map<String, Object> paramMap) {
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append(" WHERE 1 = 1 ");
        if (null != paramMap.get("sysInfoId")
            && StringUtils.isNotBlank(String.valueOf(paramMap.get("sysInfoId")))) {
            sqlBuilder.append(" AND g.sys_info_id = #{sysInfoId} ");
        }
        if (null != paramMap.get("id")
            && StringUtils.isNotBlank(String.valueOf(paramMap.get("id")))) {
            sqlBuilder.append(" AND u.id = #{id} ");
        }
        return sqlBuilder.toString();
    }

}
